package com.wnxy.sdd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.sdd.entity.Trainer;
import com.wnxy.sdd.mapper.TrainerMapper;
import com.wnxy.sdd.model.query.TrainerQuery;
import com.wnxy.sdd.model.vo.PageVo;
import com.wnxy.sdd.service.ITrainerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class TrainerServiceImpl extends ServiceImpl<TrainerMapper, Trainer>
        implements ITrainerService {


    @Override
    public PageVo<Trainer> findByPage(Integer current, Integer size, TrainerQuery trainerQuery) {
        // 构造分页条件对象
        IPage<Trainer> iPage = new Page<>(current,size);

        // 构造查询条件
        LambdaQueryWrapper<Trainer> wrapper = Wrappers.lambdaQuery(Trainer.class);
        if (trainerQuery != null) {
            if (StringUtils.hasText(trainerQuery.getName())) {
                wrapper.like(Trainer::getName,trainerQuery.getName());
            }
            if (StringUtils.hasText(trainerQuery.getIntro())) {
                wrapper.like(Trainer::getIntro,trainerQuery.getIntro());
            }
            if (StringUtils.hasText(trainerQuery.getCareer())) {
                wrapper.like(Trainer::getCareer,trainerQuery.getCareer());
            }
        }

        // 分页查询
        IPage<Trainer> page = this.page(iPage, wrapper);

        return PageVo.<Trainer>builder()
                .list(page.getRecords())
                .total(page.getTotal())
                .build();
    }
}
